<?php
class User {
  var $sessionName = "X2_USER";
  var $userInfo = array();
  var $authorized = false;

  function User(){
    $this -> setDefault();

    if(isset($_COOKIE[$this -> sessionName])){
      session_name($this -> sessionName);
      session_start();
      if(isset($_SESSION[$this -> sessionName])){
        $this -> userInfo = $_SESSION[$this -> sessionName];
      }
      $this -> authorized = true;
    }
  }

  function &getInstance(){
    static $instance;
    if(!isset($instance)){
      $instance = new User;
    }
    return $instance;
  }

  function login($login, $pass, $remember = false){
    $userInfo = $this -> getUserInfo($login, $pass);
    if($userInfo === false) return false;

    session_name($this -> sessionName);
 /*   if($remember){
      session_set_cookie_params(60*60*24*180);
    }*/
    session_start();
    $_COOKIE[session_name()] = session_id();
    $this -> userInfo = $userInfo;
    $this -> authorized = true;
    $_SESSION[$this -> sessionName] = $this -> userInfo;
    return true;
  }

  function logout() {
    session_name($this -> sessionName);
    unset($_SESSION[$this -> sessionName]);
    unset($_COOKIE[session_name()]);
    $this -> setDefault();
    $this -> authorized = false;
    setcookie(session_name(), '', time() - 3600, "/");
    session_destroy();
  }

  function setDefault(){
    $this -> userInfo = array(
      'login' => '',
      'pass' =>  '',
      'status' => 0,
      'owner' => 0,
      'create' => 0,
      'update' => 0,
      'remove' => 0,
      'comment' => 1,
      'moderate' => 0,
      'name' => 'Посетитель',
      'email' => ''
    );
  }

  function register($data){
    if($this -> userExists($data['login'], $data['pass'])) {
      return false;
    }
    return $this -> registerUser($data);
  }

  function isAuthorized(){
    return $this -> authorized;
  }

  // Выборки
  function getUserInfo($login, $pass){
    $this -> loadInfo();
    return $this -> userExists($login, $pass)? $this -> users[$login] : false;
  }

  function userExists($login, $pass){
    if(count($this -> users) == 0){
      $this -> loadInfo();
    }
    return isset($this -> users[$login]) && $this -> users[$login]['pass'] == md5($pass);
  }

  function loadInfo(){
    $this -> users = array();
  	$this -> usersPath = array(
      realpath(X2 ."/users.inc.php"),
      realpath(X2_PROJECT ."/users.inc.php")
    );

    foreach($this -> usersPath as $path){
      if($path !== false){
  	    $this -> users = array_merge($this -> users, include($path));
  	    break;
      }
    }
  }

  function toString(){
    $xml  = '<x:user xmlns:x="'. X2_NS .'"';
    $xml .= ' authorized="'. intval($this -> authorized) .'"';
    $xml .= ' status="'. intval($this -> userInfo['status']) .'"';
    $xml .= ' create="'. intval($this -> userInfo['create']) .'"';
    $xml .= ' update="'. intval($this -> userInfo['update']) .'"';
    $xml .= ' remove="'. intval($this -> userInfo['remove']) .'"';
    $xml .= ' comment="'. intval($this -> userInfo['comment']) .'"';
    $xml .= ' moderate="'. intval($this -> userInfo['moderate']) .'"';
    $xml .= '>';
    $xml .= '<x:name>'. htmlspecialchars($this -> userInfo['name'], ENT_NOQUOTES) .'</x:name>';
    $xml .= '<x:login>'. htmlspecialchars($this -> userInfo['login'], ENT_NOQUOTES) .'</x:login>';
    $xml .= '<x:email>'. htmlspecialchars($this -> userInfo['email'], ENT_NOQUOTES) .'</x:email>';
    $xml .= '</x:user>';
    return $xml;
  }
}
?>